import mysql  from 'mysql'
import dotenv  from 'dotenv'
dotenv.config()


const basicConfig = {
    host: process.env.DBHOST,
    port: process.env.DBPORT,
    connectTimeout: 5000,
    admin: process.env.ADMIN,
    password: process.env.PASSWORD,
    database: process.env.DATABASE
}

const DataBaseConfig = Object.assign({}, basicConfig)
const pool = mysql.createPool({
    host: DataBaseConfig.host,
    user: DataBaseConfig.admin,
    password:DataBaseConfig.password,
    port: DataBaseConfig.port,
    database: DataBaseConfig.database || null
})


pool.getConnection((err, connection) => {
    if (err) throw Error('连接MYSQL失败~~~',err)
    else {
        connection.query('select 1 + 1 as solution',(err,rows) => {
            if (err) throw Error('查询失败',err) 
            else {
                console.log('连接数据库成功~~~', rows);
            }
        })
    }

    connection.release()
})

const connection = pool

export default connection